<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2020 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\cms\controller;


use function PHPSTORM_META\type;
use think\admin\Controller;
use think\facade\Config;
use think\admin\service\AdminService;
use think\admin\extend\DataExtend;
use think\admin\service\MenuService;




/**
 * 分班管理
 * Class EduClass
 * @package app\wechat\controller
 */
class Edugroup extends BaseController
{

    /**
     * 当前操作数据库
     * @var string
     */
    protected $table = 'CmsEduClassGroup';
    static $STATE_OPTION = array(0=>"未开启报名",1=>"报名中",2=>"开课中",3=>"培训完毕",9=>"已取消");
    public $type = array(1=>"岗位培训",2=>"技能培训",3=>"其它培训");
    public $scale = array(1=>"1-20人小班",2=>"20-50人中班",3=>"100人以上大班");
    public $superviser = array(1=>"长沙市局",2=>"湖南省厅",3=>"娄底市局");
    public $city = array(1=>"长沙市",2=>"湖南省",3=>"娄底市");

    /**
     * 分班管理
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $this->title = "班级管理";
        $this->class_id = $this->request->get("class_id");
        $map = ['class_id'=>$this->class_id];
        $this->_query($this->table)->like('name,addr')->where($map)->order('id desc')->page();
    }

    /**
     * 表单数据处理
     * @param array $vo
     * @throws \ReflectionException
     */
    protected function _form_filter(&$vo)
    {
        $this->class_id = $this->request->request("class_id");
        if ($this->request->isGet()) {
            // 清理权限节点
            AdminService::instance()->clearCache();
        }
    }


    /**
     * 添加分班
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function add()
    {
        $this->class_id = $this->request->request("class_id");
        if ($this->request->isGet()) {
            $this->courses = [];
            $this->_applyFormToken();
            $this->_form($this->table, 'form');
        } else {
            $update = $this->request->post('data', []);
            $update[ 'create_by']   = AdminService::instance()->getUserId();
            $update[ 'update_at'] = $update[ 'create_at'] = time();
            $id = $this->app->db->name($this->table)->insertGetId($update);
            if ($id !== false) {
                $this->success('添加成功！','javascript:history.back()');
            } else {
                $this->error('添加失败，请稍候再试！');
            }
        }
    }

    /**
     * 编辑分班信息
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        $this->id = $this->request->get('id');
        $map = array("id" => $this->id);
        if (empty($this->id)) $this->error('参数错误，请稍候再试！');
        if ($this->request->isGet()) {
            if ($this->request->get('output') === 'json') {
                $data = $this->app->db->name($this->table)->where($map)->find();
                $this->success('获取数据成功！', $data);
            } else {
                $this->title = '编辑信息';
                $this->_applyFormToken();
                $this->_form($this->table, 'form');
            }
        }else{
            $data = $this->request->post('data', []);
            $data['update_at'] = time();
            if ($this->app->db->name($this->table)->where($map)->update($data) !== false) {
                $this->success('信息更新成功！', 'javascript:history.back()');
            } else {
                $this->error('信息更新失败，请稍候再试！');
            }
        }
    }

    /**
     * 删除分班信息
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function remove()
    {
        $this->_applyFormToken();
        $this->_delete($this->table);
    }

    /**
     * 导入分班信息
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function import(){
        //$inputFileName = "F:\\phpStudy\\GasPortalProject\\temp\\01simple.xlsx";
        //$inputFileName = "http://127.0.0.1:9500/upload/63/1fcfb478ee9a562f929784a1b44e64.xlsx";
        $class_id = $this->request->request("class_id");
        $inputFileName = $this->request->request("url");
        $inputFileName = $this->filerFilePath($inputFileName);

        $fields = [ "name", "addr", "contact_man", "contact_phone" , "time_str", "notes" ];
        $data = $this->getImportDataFromExcel($inputFileName, $fields);

        //检查报名号是否有效
        for($i=0; $i<count($data); $i++){
            $time = explode(" - ", $data[$i]['time_str']);
            if(count($time)!=2){
                $this->error("错误的时间格式");
            }
        }

        //将数据进行导入
        for($i=0; $i<count($data); $i++){
            $toImport = $this->_prepareImportData($data[$i],$class_id);
            $this->app->db->name($this->table)->save($toImport);
        }
        $this->success("导入成功");
    }

    public function _prepareImportData($data,$class_id){
        $time = explode(" - ", $data['time_str']);
        $data['start_at'] = $time[0];
        $data['end_at'] = $time[1];
        $data['class_id'] = $class_id;
        return $data;
    }

}
